package com.equipment;

import java.util.NoSuchElementException;
import android.annotation.SuppressLint;
import com.util.EncodingUtil;

/**
 * B4取随机数响应帧
 * @version 1.18
 * @author 刘亚东
 */
@SuppressLint("DefaultLocale")
public class GetRandomResponse {
	// 帧开始标记
	private String stx = "55AA";
	// 串口帧序列号
	private String rsctl = "00";
	// 长度
	private String Length = "";
	// 代码类型
	private String code = "DF";
	// 拓展子码
	private String subCode = "B4";
	// 获取随机数类型
	private String dir = "01";
	// 获取成功是否
	private String status = "00";
	// 随机数长度
	private String randomLen = "";
	// 随机数
	private String random = "";
	// 异或校验值
	private String bcc = "ec";

	/**
	 * 解析B4取随机数响应帧
	 *
	 * @param context
	 * @return
	 */
	public GetRandomResponse analysis(String context)
			throws NoSuchElementException {
		// 声明一个对象来封装属性
		GetRandomResponse or = null;
		try {
			if (!context.substring(12, 14).equals("B4")) {
				throw new NoSuchElementException("取随机数失败");
			}
			// 判断是否成功
			if (!context.substring(14, 16).equals("00")) {
				throw new NoSuchElementException("取随机数失败");
			}
			// 截取到BCC校验值 需要判断校验是否相等
			bcc = context.substring(context.length() - 2).toUpperCase();
			// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)
			String str = context.substring(4, context.length() - 2);
			// 得到比对校验
			str = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
			if (!str.equals(bcc)) {
				throw new NoSuchElementException("取随机数失败");
			}
			or = new GetRandomResponse();
			if(context.substring(context.length() - 6,context.length()-2).equals("9000")){
				throw new NoSuchElementException("取随机数失败");
			}
			or.setRandom(context.substring(20, context.length() - 2));
		} catch (Exception e) {
			throw new NoSuchElementException("取随机数失败");
		}
		return or;
	}

	public String getStx() {
		return stx;
	}

	public void setStx(String stx) {
		this.stx = stx;
	}

	public String getRsctl() {
		return rsctl;
	}

	public void setRsctl(String rsctl) {
		this.rsctl = rsctl;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getSubCode() {
		return subCode;
	}

	public void setSubCode(String subCode) {
		this.subCode = subCode;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getRandomLen() {
		return randomLen;
	}

	public void setRandomLen(String randomLen) {
		this.randomLen = randomLen;
	}

	public String getRandom() {
		return random;
	}

	public void setRandom(String random) {
		this.random = random;
	}

	public String getBcc() {
		return bcc;
	}

	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	public String getLength() {
		return Length;
	}

	public void setLength(String length) {
		Length = length;
	}

}
